package cn.antblog.code.test1;

import java.util.ArrayList;
import java.util.List;

public enum LableEnum {

    YIFU("衣服", 1),
    KUZI("裤子", 2),
    XIEZI("鞋子", 4),
    MAOZI("帽子", 8),
//    YIFU("衣服", 1),
//    KUZI("裤子", 2),
//    XIEZI("鞋子", 3),
//    MAOZI("帽子", 4),

    ;

    private String lableName;

    private int code;

    LableEnum(String lableName, int code) {
        this.lableName = lableName;
        this.code = code;
    }

    public String getLableName() {
        return lableName;
    }

    public int getCode() {
        return code;
    }

    /**
     * 通过数据库中存储的位运算结果反向获取枚举集合
     *
     * @param code
     * @return
     */
    public static List<String> getLableNames(int code) {
        LableEnum[] lableEnums = LableEnum.values();
        List<String> result = new ArrayList<>();
        for (LableEnum lableEnum : lableEnums) {
            if ((lableEnum.getCode() & code) != 0) {
                result.add(lableEnum.getLableName());
            }
        }
        return result;
    }
}
